package com.example.baseadapter;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        intiView();
        String name=Singleton.getInstance().getName();
    }
    private  void intiView(){
        //获取ListView
        ListView contactList=findViewById(R.id.list_view);
        //构造数据
        List<ContactModel> list=new ArrayList<>();
        for (int i=0;i<20;i++){
            ContactModel model=new ContactModel();
            model.setContactName("张三"+i);
            model.setIconResId(R.mipmap.ic_launcher);
            list.add(model);
        }
        MyAdpater adpater=new MyAdpater(this,list);
        contactList.setAdapter(adpater);

        adpater.setListener(new MyAdpater.OnContactItemIconClick() {
            @Override
            public void onContactItemIconClick(int position) {
                Toast.makeText(MainActivity.this,"点击了"+list.get(position).getContactName()+"的头像",
                        Toast.LENGTH_SHORT).show();
            }
        });
        //ListView的条目点击事件
        contactList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            //position 列表脚标 对应数据list脚标
            //view即适配器adapter中getView()返回的列表条目对象
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                //ListView列表条目点击事件的目的是：获取用户点击条目所对应的数据list中数据
                String name=list.get(position).getContactName();
                Toast.makeText(MainActivity.this,"联系人姓名："+name,Toast.LENGTH_SHORT).show();
            }
        });
        //ListView列表条目长按事件
        contactList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, int position, long id) {
                String name=list.get(position).getContactName();
                Toast.makeText(MainActivity.this,"联系人姓名："+name,Toast.LENGTH_SHORT).show();
                return true;
            }
        });
    }

}